package com.goldcn.classify.service;

import com.goldcn.classify.dao.StyleMapper;
import com.goldcn.classify.dto.Dto_StyleClazz;
import com.goldcn.classify.model.Classify;
import com.goldcn.classify.model.Style;
import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.UuidUtils;
import com.goldcn.enumeration.DelFlagEnum;
import com.goldcn.enumeration.ErrorCodeEnum;
import com.goldcn.util.CommonTools;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/9.
 */
@Service("styleService")
@Transactional
public class StyleService {

    @Autowired
    private StyleMapper styleMapper;

    private Logger logger=Logger.getLogger(StyleService.class);

    /**
     * 根据平台类型查询款式列表
     * @param code
     * @return
     */
    public List<Style> loadStyleList(String code){
        //0、通用1、2、3、各种平台4、所有数据5 ERP
        try{
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("styleisstop",DelFlagEnum.NO.getValue());
            param.put("styletype",code);
            return styleMapper.loadStyleList(param);
        }catch (Exception e){
            throw CommonTools.createException(ErrorCodeEnum.parmError);
        }
    }

    /**
     * 根据平台类型查询款式-品类列表
     * @param code
     * @return
     */
    public List<Dto_StyleClazz> selectClassifyByStyle(String code){
        try{
            return styleMapper.selectClassifyByStyle(code);
        }catch (Exception e){
            logger.error(e.getMessage());
            throw CommonTools.createException(ErrorCodeEnum.parmError);
        }
    }

    /**
     * 查询款式详情 List
     * @param styleList
     * @return
     */
    public List<Style> detailList(List<String> styleList) {
        return styleMapper.selectListByPrimaryKey(styleList);
    }

    /*************************************/



    public int deleteStyle(String id) {
        Style record = new Style();
        record.setStyleid(id);
        record.setStyleisstop(DelFlagEnum.YES.getValue());
        return styleMapper.updateByPrimaryKeySelective(record);
    }

    public int addStyle(Style record) {
        record.setStyleid(UuidUtils.uuid());
        record.setStyleisstop(DelFlagEnum.NO.getValue());
        return styleMapper.insertSelective(record);

    }

    public Style detail(String styleid) {
        return styleMapper.selectByPrimaryKey(styleid);
    }

    public int updateStyle(Style record) {
        if (styleMapper.selectByPrimaryKey(record.getStyleid())==null) {
            throw new ServiceException("无修改对象信息", RestConst.ErrorCode.DATABASE_ERROR);
        }
        return styleMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Style record) {
        record.setStyleisstop(DelFlagEnum.NO.getValue());
        return styleMapper.updateByPrimaryKey(record);
    }



    public Style checkStyle(String id){
        return  styleMapper.selectByPrimaryKey(id);
    }

}
